package com.example.administrator.qq;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.DB.DBManager;
import com.example.administrator.VO.UserInfo;
import com.example.administrator.VO.UserPoint;

import java.util.Date;

import static com.example.administrator.Util.ShowToastTime.showMyToast;

/**
 * Created by ykbboom on 2016/12/29.
 * Modified by tianqi,ykbboom on 2017/1/3
 */
public class MainActivity extends AppCompatActivity {

    private DBManager dbManager;
    private TextView bodyNumText;//显示体力数
    private TextView moneyNumText;//显示金币数
    private Button settingBtn;//设置按钮
    private Button startBtn ;//开始游戏
    private Button rankBtn ;//排行榜
    private Button signInBtn;//签到
    private Button storeButton;//商店
    private Button acmtButton;//成就
    private ImageButton leftButton;//切换人物
    private ImageButton rightButton;//切换人物
    private ImageView actor;//人物
    int flag = 0;//判断切换人物的标志
    long signInInterval = 2*1000;//签到间隔5秒钟
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbManager = new DBManager(this);
        acmtButton=(Button)findViewById(R.id.achievement);//成就
        bodyNumText = (TextView) findViewById(R.id.textView3);//显示体力数
        moneyNumText = (TextView) findViewById(R.id.textView5);//显示金币数
        settingBtn = (Button) findViewById(R.id.button5);//设置按钮
        startBtn = (Button) findViewById(R.id.button3);//开始游戏
        rankBtn = (Button) findViewById(R.id.button8);//排行榜
        signInBtn = (Button) findViewById(R.id.sign_in);//签到
        storeButton=(Button)findViewById(R.id.store);//商店
        leftButton=(ImageButton)findViewById(R.id.button_left);//切换人物
        rightButton=(ImageButton)findViewById(R.id.button_right);//切换人物
        actor=(ImageView)findViewById(R.id.acotor_image);//获取并显示人物图片
        actor.setImageResource(R.drawable.a);
        /*
        * 添加背景音乐
        * */
        Intent intent = new Intent("com.angel.Android.MUSIC");
        intent.setPackage(getPackageName());
        startService(intent);

        /*
        * 查询数据库，如果为空则插入一条数据，否则返回查询到的数据集合
        * */
        UserInfo userInfo = dbManager.queryAll();
        if (userInfo.get_id()==0){//如果当前表没有任何数据
            userInfo = UserInfo.initializeUser();//初始化为_id 1，体力数 5，金币数 10,用户名 PlayerOne
            UserPoint userPoint = UserPoint.initializeUserPoint(1,0,0);//初始化为用户id 1，计时分数 0，限步分数 0
            UserPoint userPoint1 = UserPoint.initializeUserPoint(1,0,0);
            UserPoint userPoint2 = UserPoint.initializeUserPoint(1,0,0);
            UserPoint userPoint3 = UserPoint.initializeUserPoint(1,0,0);
            UserPoint userPoint4 = UserPoint.initializeUserPoint(1,0,0);
            dbManager.addUser(userInfo);
            dbManager.addUserPoint(userPoint);//初始化排行榜前五
            dbManager.addUserPoint(userPoint1);
            dbManager.addUserPoint(userPoint2);
            dbManager.addUserPoint(userPoint3);
            dbManager.addUserPoint(userPoint4);
            UserInfo userInfoNew = dbManager.queryAll();
            bodyNumText.setText(String.valueOf(userInfoNew.getBody_num()));
            moneyNumText.setText(String.valueOf(userInfoNew.getMoney_num()));
        }else {
            bodyNumText.setText(String.valueOf(userInfo.getBody_num()));
            moneyNumText.setText(String.valueOf(userInfo.getMoney_num()));
        }

        leftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (flag) {
                    case 0:
                        actor.setImageResource(R.drawable.a2);
                        flag = 2;
                        break;
                    case 1:
                        actor.setImageResource(R.drawable.a);
                        flag = 0;
                        break;
                    case 2:
                        actor.setImageResource(R.drawable.a1);
                        flag = 1;
                        break;
                    default:
                        break;
                }
            }
        });
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (flag) {
                    case 0:
                        actor.setImageResource(R.drawable.a1);
                        flag = 1;
                        break;
                    case 1:
                        actor.setImageResource(R.drawable.a2);
                        flag = 2;
                        break;
                    case 2:
                        actor.setImageResource(R.drawable.a);
                        flag = 0;
                        break;
                    default:
                        break;
                }
            }
        });
        //跳转到成就
        acmtButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,AchievementActivity.class);
                startActivity(intent);
            }
        });
        //跳转到设置界面
        settingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,SettingActivity.class);
                startActivity(i);
            }
        });
        //跳转到签到
        signInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Date date = new Date();
                long dateTime = date.getTime();
                //查询用户上一次的签到时间(毫秒)
                long preTime = dbManager.queryAll().getSign_in_time();
                //如果过去了30分钟
                if((dateTime-preTime)>=signInInterval){
                    //进行签到
                    UserInfo userInfo1 = dbManager.queryAll();
                    dbManager.updateSignInTime(userInfo1.get_id(),dateTime);
                    //加一金币
                    userInfo1.setMoney_num(userInfo1.getMoney_num()+1);
                    dbManager.updateMoneyNum(userInfo1);
                    Toast toast=Toast.makeText(MainActivity.this, "获得一金币~", Toast.LENGTH_LONG);
                    showMyToast(toast,1*1000);
                    //设置最新金币数
                    moneyNumText.setText(String.valueOf(userInfo1.getMoney_num()));
                }else{
                    Toast toast=Toast.makeText(MainActivity.this, "过会儿再来签到哦~", Toast.LENGTH_LONG);
                    showMyToast(toast,1*1000);
                }
            }
        });
        //跳转到开始游戏界面
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i2 = new Intent(MainActivity.this,ModelActivity.class);
                startActivity(i2);
                MainActivity.this.finish();
            }
        });
        //跳转到排行榜界面
        rankBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,RankActivity.class);
                startActivity(i);
            }
        });
        //跳转到商店界面
        storeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,StoreActivity.class);
                startActivityForResult(intent,1);
            }
        });

    }
    /*
    * 从商店返回主界面后刷新体力数和金币数
    * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==1){
            dbManager = new DBManager(this);
            UserInfo userInfo = dbManager.queryAll();
            bodyNumText = (TextView) findViewById(R.id.textView3);//显示体力数
            moneyNumText = (TextView) findViewById(R.id.textView5);//显示金币数
            bodyNumText.setText(String.valueOf(userInfo.getBody_num()));
            moneyNumText.setText(String.valueOf(userInfo.getMoney_num()));
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        //应用的最后一个Activity关闭时应释放DB
        dbManager.closeDB();
    }
}
